package developer.zyk.global.upload;

import cn.hutool.core.collection.CollUtil;
import developer.zyk.global.upload.dao.entity.ChatObjectRepositoryDO;
import developer.zyk.global.upload.dao.mappers.ChatObjectRepositoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author zhangyongkang
 * @time 2025/5/15 15:09
 * @description
 */
@Component
public class FileUtil {

    private static ChatObjectRepositoryMapper chatObjectRepositoryMapper;

    @Autowired
    public void setChatObjectRepositoryMapper(ChatObjectRepositoryMapper mapper) {
        FileUtil.chatObjectRepositoryMapper = mapper;
    }

    public static ChatObjectRepositoryDO getFileByFileId(Long id) {
        return chatObjectRepositoryMapper.selectById(id);
    }

    public static List<ChatObjectRepositoryDO> getFileByFileIs(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return chatObjectRepositoryMapper.selectByIds(ids);
    }

    public static Map<Long, ChatObjectRepositoryDO> getFileMapByFileIs(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new HashMap<>();
        }
        return chatObjectRepositoryMapper.selectByIds(ids)
                .stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(ChatObjectRepositoryDO::getId, Function.identity(), (a, b) -> b));
    }
}
